home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Programmer Disk
/
The Programmer Disk (Microforum).iso
/
xpro
/
tutor
/
pro18
/
keyboard.qb4
< prev
next >
Wrap
Text File
|
1991-10-14
|
3KB
|
104 lines
'KEYBOARD.BAS - From the GWBASIC Tutorial Series, #11
'October, 1991
'To illustrate key trapping, let's use a simple little program that just
'prints a message on the screen, changing the color every once in a while,
'then when one of the 'trapped' keys is pressed beeps and prints a message
'at the bottom of the screen.
'First, we define the 'user' keys:
KEY 16, CHR$(&H8) + CHR$(&H1E)
KEY 17, CHR$(&H8) + CHR$(&H20)
KEY 18, CHR$(&H8) + CHR$(&H21)
KEY 19, CHR$(&H8) + CHR$(&H19)
KEY 20, CHR$(&H0) + CHR$(&H1)
'Next, we need to turn on key trapping...
KEY(1) ON
KEY(13) ON
KEY(12) ON
KEY(16) ON
KEY(17) ON
KEY(18) ON
KEY(19) ON
KEY(20) ON
'Finally, tell the program what to do with these keys...
ON KEY(1) GOSUB F1Key
ON KEY(13) GOSUB RightArrow
ON KEY(12) GOSUB LeftArrow
ON KEY(16) GOSUB AltA
ON KEY(17) GOSUB AltD
ON KEY(18) GOSUB AltF
ON KEY(19) GOSUB AltP
ON KEY(20) GOSUB Escape
'Here's where your main program would go. Instead, we'll have some fun!
Start:
CLS : KEY OFF
RANDOMIZE (-TIMER)
WaitLoop:
TIME = RND(500)
LOCATE 15, 1, 0
COL = COL + 1
IF COL > 16 THEN COL = 1
COLOR COL, 0, 0
PRINT "I'm waiting for you to press a key..."
FOR N = 1 TO TIME: NEXT N
GOTO WaitLoop
END
'Key trapping subroutines are here...
F1Key:
' F1 key pressed - print a message
LOCATE 20, 1, 0
PRINT "The last key you pressed was the F1 key... "
RETURN
RightArrow:
' Right arrow pressed - print a message
LOCATE 20, 1, 0
PRINT "The last key you pressed was the right arrow key... "
RETURN
LeftArrow:
' Left arrow pressed - print a message
LOCATE 20, 1, 0
PRINT "The last key you pressed was the left arrow key... "
RETURN
AltA:
' Alt-A key pressed - print a message
LOCATE 20, 1, 0
PRINT "The last key you pressed was the Alt-A key... "
RETURN
AltD:
' Alt-D key pressed - print a message
LOCATE 20, 1, 0
PRINT "The last key you pressed was the Alt-D key... "
RETURN
AltF:
' Alt-F key pressed - print a message
LOCATE 20, 1, 0
PRINT "The last key you pressed was the Alt-F key... "
RETURN
AltP:
' Alt-P key pressed - print a message
LOCATE 20, 1, 0
PRINT "The last key you pressed was the Alt-P key... "
RETURN
Escape:
' Escape key pressed - print a message
LOCATE 20, 1, 0
PRINT "The last key you pressed was the ESCAPE key... "
RETURN